package com.qdxht.dispenser.ui.activity;

import android.annotation.SuppressLint;
import android.graphics.Color;
import android.os.Handler;
import android.os.Message;
import android.text.Spannable;
import android.text.SpannableStringBuilder;
import android.text.TextPaint;
import android.text.TextUtils;
import android.text.method.LinkMovementMethod;
import android.text.style.ClickableSpan;
import android.text.style.UnderlineSpan;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import com.qdxht.dispenser.R;
import com.qdxht.dispenser.base.AppApplication;
import com.qdxht.dispenser.base.BaseActivity;
import com.qdxht.dispenser.databinding.ActivitySplashBinding;
import com.qdxht.dispenser.ui.activity.user.LoginActivity;
import com.qdxht.dispenser.utils.SpUtils;
import com.qdxht.dispenser.utils.UserUtil;
import com.qdxht.dispenser.utils.ViewUtil;

/**
 *    author : Android 轮子哥
 *    github : https://github.com/getActivity/AndroidProject
 *    time   : 2018/10/18
 *    desc   : 闪屏界面
 */
public final class SplashActivity extends BaseActivity<ActivitySplashBinding> {

    @Override
    protected ActivitySplashBinding getBinding() {
        return ActivitySplashBinding.inflate(getLayoutInflater());
    }

    int count = 3;

    @SuppressLint("HandlerLeak")
    private Handler mHandler = new Handler() {
        @SuppressWarnings("unused")
        public void handleMessage(Message msg) {
            switch (msg.what) {
                case 100: {
                    count--;
                    if(count < 0){
                        toPage();
                    } else {
                        ViewUtil.setText(binding.tvAd, count+"s");
                        mHandler.sendEmptyMessageDelayed(100,1000);
                    }
                    break;
                }
                default:
                    break;
            }
        };
    };

    @Override
    protected void initView() {
        Boolean first = SpUtils.decodeBoolean("first");
        if(!first){
            showAlert();
            return;
        }
        // 加载第三方SDK
        ((AppApplication)getApplication()).initLazySdk();
//        toPage();

        mHandler.sendEmptyMessageDelayed(100,1000);
    }

    @Override
    protected void initData() {
    }

    protected void toPage() {
        UserUtil.removeLoginEntity();
        UserUtil.removeUserInfo();
        LoginActivity.start(mActivity);
        finish();
    }

    private void showAlert() {
        try {
            AlertDialog.Builder builder = new AlertDialog.Builder(mActivity);
            View view = LayoutInflater.from(this).inflate(R.layout.dialog_notice_message, null);
            TextView tv_text = view.findViewById(R.id.tv_text);
            TextView yes = (TextView) view.findViewById(R.id.yes);
            TextView no = (TextView) view.findViewById(R.id.no);
            builder.setView(view);
            tv_text.setText(updateTextStyle(tv_text.getText().toString()));
            tv_text.setMovementMethod(LinkMovementMethod.getInstance());

            final AlertDialog dialog = builder.create();
            dialog.setCanceledOnTouchOutside(false);
            dialog.setCancelable(false);
            dialog.show();
            yes.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    SpUtils.encode("first", true);
                    dialog.dismiss();
                    ((AppApplication)getApplication()).initLazySdk();
                    toPage();
                }
            });

            no.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    //保存本地
                    dialog.dismiss();
                    android.os.Process.killProcess(android.os.Process.myPid());
                }
            });
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private SpannableStringBuilder updateTextStyle(String content) {
        SpannableStringBuilder spannableString = new SpannableStringBuilder();
        spannableString.append(content);

        //使用ForegroundColorSpan添加点击事件会出现冲突
        UnderlineSpan colorSpan = new UnderlineSpan() {
            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setColor(Color.parseColor("#fe9500"));//设置颜色
                ds.setUnderlineText(false); //去掉下划线
            }
        };

        //使用UnderlineSpan很好的兼容这个问题
        UnderlineSpan colorSpan1 = new UnderlineSpan() {
            @Override
            public void updateDrawState(TextPaint ds) {
                ds.setColor(Color.parseColor("#fe9500"));//设置颜色
                ds.setUnderlineText(false); //去掉下划线
            }
        };

        int privacyBeginIndex = content.indexOf("《");
        int privacyEndIndex = content.indexOf("》") + 1;
        int protocolBeginIndex = content.lastIndexOf("《");
        int protocolEndIndex = content.lastIndexOf("》") + 1;

        ClickableSpan privacyClickableSpan = new ClickableSpan() {

            @Override
            public void onClick(@NonNull View widget) {
                WebActivity.start(mActivity, 0);
            }
        };

        ClickableSpan protocolClickableSpan = new ClickableSpan() {

            @Override
            public void onClick(@NonNull View widget) {
                WebActivity.start(mActivity, 1);
            }
        };

        spannableString.setSpan(protocolClickableSpan, protocolBeginIndex, protocolEndIndex, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        spannableString.setSpan(privacyClickableSpan, privacyBeginIndex, privacyEndIndex, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

        //字体颜色一定要放在点击事件后面，不然部分手机不会修改颜色
        spannableString.setSpan(colorSpan, protocolBeginIndex, protocolEndIndex, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);
        spannableString.setSpan(colorSpan1, privacyBeginIndex, privacyEndIndex, Spannable.SPAN_EXCLUSIVE_INCLUSIVE);

        return spannableString;
    }

}